﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Efcore.MultiTenant.DbProvider;
using Microsoft.EntityFrameworkCore;

namespace Efcore.MultiTenant
{
    public class BaseDbContext : DbContext
    {
        private readonly IServiceProvider _serviceProvider;

        /// <summary>
        /// 多租户信息
        /// </summary>
        public TenantInfo TenantInfo { get; }

        public BaseDbContext(DbContextOptions options, TenantInfo tenantInfo, IServiceProvider serviceProvider)
            :base(options)
        {
            TenantInfo = tenantInfo;
            _serviceProvider = serviceProvider;
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            var builderType = typeof(IDbProvider<>).MakeGenericType(this.GetType());
            var dbProvider = (IDbProvider)_serviceProvider.GetService(builderType);
            dbProvider.OnModelCreating(modelBuilder);
        }
    }
}
